package com.xiyue.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.sport.model.entity.SysPermission;
import com.xiyue.server.mapper.SysPermissionMapper;
import com.xiyue.server.service.SysPermissionService;

import java.util.List;

/**
 * 权限表;(sys_permission)表服务实现类
 * @author: xiyue
 * @date : 2025-8-10
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    @Resource
    private SysPermissionMapper sysPermissionMapper;

    @Override
    public void tree(List<SysPermission> records) {
        // 分页查询到各个父级菜单，从父级菜单开始查询
        for (SysPermission record : records) {
            record.setLabel(record.getName());
            record.setValue(record.getId());
            LambdaUpdateWrapper<SysPermission> lqw = new LambdaUpdateWrapper<>();
            lqw.eq(SysPermission::getParentId, record.getId());
            lqw.orderByAsc(SysPermission::getSort);
            List<SysPermission> list = super.list(lqw);
            /**
             *  由于子集可能还存在子集，需进行递归查询
             *      将存在的子子集数据封装进子集中
             */
            if(!list.isEmpty()){
                record.setChildren(list);
                // 递归查询子级并装在到children字段中
                tree(list);
            }
        }
    }
}